#写文件实例

'''
r 只读
a 追加
w 写入
close 操作完毕关闭
read(xx)   xx参数代表读取几个字符  不加参数读取全部
redilnes   该方法会返回一个列表，列表元素对应文本文件中每行内容
splitlines 不要换行符
'''

#追加
# f = open('./tmp.txt','a',encoding='utf8')
# f.write(':电子科技')
# f.close()

#读取
# f = open('./tmp.txt','r',encoding='utf8')
# conent = f.read()
# f.close()
# name =conent.split(':')[0]
# print(name)

# readlines  会有换行符
# f = open('./tmp.txt','r',encoding='utf8')
# conent = f.readlines()
# f.close()
# for line in conent:
#     print(line)

# splitlines 不要换行符
# f = open('./tmp.txt','r',encoding='utf8')
# conent = f.read()
# f.close()
# linelist = conent.splitlines()
# for line in conent:
#     print(line)

# with open python智能调用close方法
# with open('./tmp.txt','r',encoding='utf8') as f:
#     content = f.readlines()
#     for line in content:
#         print(line)

'''openpyxl  读写excel'''

import openpyxl
import xlrd

# 创建一个Excel workbook对象
book = openpyxl.Workbook()
# 创建时，会自动产生一个sheet。通过active获取
sh = book.active
# 修改当前 sheet 标题
sh.title = '工资表'
# # 保存
# book.save('./info.xls')

sh1 = book.create_sheet('年龄表-最后') # 增加一个sheet 在最后
sh2 = book.create_sheet('年龄表-最前',0) # 增加一个sheet 在最前 索引0
sh3 = book.create_sheet('年龄表2',1)    # 增加一个sheet 为第二个表单

# 根据名称获取某个sheet对
sh = book['工资表']
# 写入内容

print(sh['A1'].value)
print(sh.cell(2,2).value)


'''读取excel   xlrd'''

bo = xlrd.open_workbook('info.xls')
sheet = bo.sheet_by_index(1)
print(sheet.name)



